package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Member;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IMemberService;
import com.woniu.supermarket.util.CodeCreateUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    IMemberService memberService;

    @GetMapping("/mlist")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照手机号查询条件", dataType = "String", paramType = "query", name = "memberTel"),
    })
    public ResultResponse<PageInfo<Member>> mlist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                String memberTel) {
        ResultResponse<PageInfo<Member>> result = null;
        try {
            PageInfo<Member> memberPageInfo = memberService.queryByTel(now, size, memberTel);
            result = new ResultResponse<>(200, "查询会员成功", memberPageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询会员失败");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改会员数据")
    public ResultResponse<Void> modify(@RequestBody Member member) {
        ResultResponse<Void> result = null;
        try {

            int i = memberService.modify(member);
            result = new ResultResponse<>(200, "修改会员成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改会员失败！");
        }
        return result;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加会员数据")
    public ResultResponse<Void> add(@RequestBody Member member) {
        ResultResponse<Void> result = null;
        try {
            int i = memberService.add(member);
            result = new ResultResponse<>(200, "添加会员成功！");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改会员失败！");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiImplicitParam(value = "删除会员 根据id删除", dataType = "int", name = "id", paramType = "path")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            int i = memberService.remove(id);
            result = new ResultResponse<>(200, "删除会员成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除会员失败！");
        }
        return result;
    }
    @GetMapping("/addcode")
    public ResultResponse<String> addcode() {
        ResultResponse<String> result = null;
        try {
            String code = CodeCreateUtil.createMemberCode();
            result = new ResultResponse<>(200, "生成编码成功！",code);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "生成编码失败！");
        }
        return result;
    }
}
